import Vue from 'vue'
import Vuex from 'vuex'
import '../util/stomp'
import '../util/sockjs'
Vue.use(Vuex)
const now = new Date();
const store = new Vuex.Store({
      state:{
            sessions:[],
		currentSessionId:-1,
            filterKey:'',
            stomp:null
      },
      mutations:{
            changeCurrentSessionId (state,id) {
			state.currentSessionId = id;
		},
		addMessage (state,msg) {
			state.sessions[state.currentSessionId-1].messages.push({
				content:msg,
				date: new Date(),
				self:true
			})
		},
		INIT_DATA (state) {
			// let data = localStorage.getItem('vue-chat-session');
			//console.log(data)
			// if (data) {
			//       state.sessions = JSON.parse(data);
			// }
		}
      },
      actions:{
            connect(context){
                  console.log("success");
                  context.state.stomp = Stomp.over(new SockJS('/ws/ep'));
                  context.state.stomp.connect({}, success => {
                        context.state.stomp.subscribe('/user/queue/chat', msg => {
                              console.log("`````````````",39,msg);
                        })
                  }, error => {
                        console.log("`````````````",42,error);
                  })
            },
            initData(context) {
			context.commit('INIT_DATA')
            }
      }
})

store.watch(function (state) {
      return state.sessions
},function (val) {
      console.log('CHANGE: ', val);
      localStorage.setItem('vue-chat-session', JSON.stringify(val));
},{
      deep:true/*这个貌似是开启watch监测的判断,官方说明也比较模糊*/
})

export default store;